<template>
  <div class="todo-footer" v-show="total">
    <label>
      <input type="checkbox" :checked="isAll" @change="checkAll" />
    </label>
    <span>
      <span>已完成{{ downTotal }}</span> / 全部{{ total }}
    </span>
    <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: "TodoFooter",
  data() {
    return {
      checkFlag: "",
    };
  },
  props: ["todoArr"],
  // props:['todoArr','checkAllTodo','clearAllTodo'],
  computed: {
    total() {
      return this.todoArr.length;
    },
    downTotal() {
      // return this.todoArr.reduce((pre,todo)=>{ return pre+ (todo.done ? 1:0) },0)
      const x = this.todoArr.reduce(
        (pre, todo) => pre + (todo.done ? 1 : 0),
        0
      ); //根据上一行，进一步简写
      if (x === this.todoArr.length) {
        this.checkFlag = true;
      }
      return x;
    },
    isAll() {
      return this.total === this.downTotal && this.total > 0; //返回boolean类型，判断是否已经全选中
    },
  },
  methods: {
    checkAll(e) {
      // this.checkAllTodo(e.target.checked)
      this.$emit("checkAllTodo", e.target.checked);
    },
    clearAll() {
      // this.clearAllTodo()
      this.$emit("clearAllTodo");
    },
  },
};
</script>

<style scoped>
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>